import * as echarts from '../../ec-canvas/echarts';
const util = require('../../utils/util.js');
// 新增：定义图表实例变量
let chart = null;

// 初始化函数
function initChart(canvas, width, height, dpr) {
  chart = echarts.init(canvas, null, {
    width: width,
    height: height,
    devicePixelRatio: dpr
  });
  canvas.setChart(chart);

  // 异步获取数据并更新图表
  util.request('/itf/common/hongpei/home/report/statisticalXcdData.json', {
    days: 7
  }).then(res => {
    if (res.success) {
      const option = {
        color: '#dc191c',
        title: {
          show: false
        },
        grid: {
          top: 'middle',
          height: '140'
        },
        tooltip: {
          show: true,
          trigger: 'axis'
        },
        xAxis: {
          type: 'category',
          boundaryGap: false,
          data: res.data.dateList
        },
        yAxis: {
          x: 'center',
          type: 'value',
          splitLine: {
            lineStyle: {
              type: 'dashed'
            }
          }
        },
        series: [{
          name: '订单量',
          type: 'line',
          symbol: 'none',
          sampling: 'lttb',
          areaStyle: {
            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
              {
                offset: 0,
                color: 'rgb(241, 138, 139)'
              },
              {
                offset: 1,
                color: 'rgb(255, 255, 255)'
              }
            ])
          },
          data: res.data.numList
        }]
      };

      chart.setOption(option);
    } else {
      util.showMsg({ msg: res.message || res.msg });
    }
  });
  return chart;
}
Page({
  data: {
    API_BASE_URL: util.API_BASE_URL,
    headDataList: [
      {
        title: '今日订单',
        num: 0,
        bg: '/images/index_bg1.png'
      }, {
        title: '会员人数',
        num: 0,
        bg: '/images/index_bg2.png'
      }, {
        title: '资源点数',
        num: 0,
        bg: '/images/index_bg3.png'
      }, {
        title: '课程数',
        num: 0,
        bg: '/images/index_bg4.png'
      },
    ],
    newestXcdList: [],
    xttjxlList: [],
    keyword: '',
    zyddt: {},
    ec: {
      onInit: initChart
    },
    chartData: {
      xAxis: [],
      yAxis: []
    }
  },
  onLoad(options) {
    this.getStatisticalData();
    this.queryResourceData(false);
    this.queryNewestXcdList();
  },
  //前往行程单
  goXcd(){
    wx.navigateTo({
      url: '/pagesTripbill/tripList/tripList?type=5',
    })
  },
  //前往推荐线路
  goTjxl(){
    wx.navigateTo({
      url: '/pages/tjxl/tjxl',
    })
  },
  //查看行程单详情
  lookTripBillDetail(e) {
    const billid = e.currentTarget.dataset.billid;
    wx.navigateTo({
      url: '/pagesTripbill/tripNominate/tripNominate?billid=' + billid + '&isbtn=2',
    })
  },
  //前往行程单预览
  goTripPreview(e) {
    const { billid } = e.currentTarget.dataset;
    wx.navigateTo({
      url: '/pagesTripbill/tripPreview/tripPreview?billid=' + billid + '&setName=2' + '&type=5',
    })
  },
  touchHandler: function (e) {
    lineChart.showToolTip(e, {
      // background: '#7cb5ec',
      format: function (item, category) {
        return category + ' ' + item.name + ':' + item.data
      }
    });
  },
  //获取头部数据接口
  getStatisticalData() {
    const _this = this;
    util.request('/itf/common/hongpei/home/report/statisticalData.json').then(res => {
      if (res.success) {
        let headDataList = _this.data.headDataList;
        headDataList.forEach(item => {
          if (item.title == '今日订单') {
            item.num = res.data.xcdNumber;
          } else if (item.title == '会员人数') {
            item.num = res.data.memberNumber;
          } else if (item.title == '资源点数') {
            item.num = res.data.zydNumber;
          } else if (item.title == '课程数') {
            item.num = res.data.courseNumber;
          }
        })
        _this.setData({
          headDataList
        })
      } else {
        util.showMsg({ msg: res.message || res.msg });
      }
    });
  },
  //获取资源点动态接口
  queryResourceData(search = true) {
    const _this = this;
    const { keyword } = this.data;
    if (!keyword && search) return;
    util.request('/itf/common/hongpei/home/report/queryResourceData.json', {
      keyword
    }).then(res => {
      if (res.success) {
        let result = res.data;
        result.voice_link = typeof result.voice_link === 'string' ? result.voice_link.replace(/^\//, '') : result.voice_link;
        _this.setData({
          zyddt: result
        })
      } else {
        util.showMsg({ msg: res.message || res.msg });
      }
    });
  },
  //获取最新订单、推荐线路接口
  queryNewestXcdList() {
    const _this = this;
    util.request('/itf/common/hongpei/home/report/queryNewestXcdList.json').then(res => {
      if (res.success) {
        _this.setData({
          newestXcdList: res.data.newestXcdList,
          xttjxlList: res.data.xttjxlList
        })
      } else {
        util.showMsg({ msg: res.message || res.msg });
      }
    });
  }
})
